package com.search;

import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Config;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.SearchRequest;
import com.meilisearch.sdk.model.DocumentsQuery;
import com.meilisearch.sdk.model.SearchResult;
import com.meilisearch.sdk.model.Settings;

import java.util.Arrays;


public class MeiliSearch {

    public static void main(String[] args) {
        MeiliSearch meiliSearch = new MeiliSearch();

        Client client = new Client(new Config("http://localhost:7700", "sWzBHtP_HmcsufeksUfIZvRxuskQgxZ58KGK9CB5_1Q"));

        // An index is where the documents are stored.
        Index index = client.index("movies");


//        meiliSearch.baseSearch(index);
//        meiliSearch.customSearch(index);
        meiliSearch.customSearchWithFilter(index);
    }


    public void baseSearch(Index index) {
        // Meilisearch is typo-tolerant:
        SearchResult results = index.search("carlo");
        System.out.println(results);
    }


    public void customSearch(Index index) {
        SearchResult results = (SearchResult) index.search(
                new SearchRequest("Drama")
                        .setShowMatchesPosition(true)
                        .setAttributesToHighlight(new String[]{"title"})
        );
        System.out.println(results.getHits());
    }

    public void customSearchWithFilter(Index index) {
        Settings settings = index.getSettings();

        Arrays.stream(settings.getFilterableAttributes()).map(x -> {
            System.out.println(x);
            return x;
        });
//        DocumentsQuery query = new DocumentsQuery()
//                .setFilter(new String[] {"(rating > 3 AND (genres = Adventure OR genres = Fiction))"})
//                .setFields(new String[] {"title", "genres", "rating", "id"})
//                .setLimit(3);
//        index.getDocuments(query, String.class);
//        index.getDocuments(query, TargetClassName.class);

    }

}
